from tools.listNode import listToNode, ListNode, nodeToList


def addTwoNumbers(l1, l2):
    next = 0
    res = ListNode()
    cur = res
    while l1 and l2:
        total = l1.val + l2.val + next
        cur.next = ListNode(total % 10)
        next = total // 10
        l1 = l1.next
        l2 = l2.next
        cur = cur.next
    while l1:
        total = l1.val + next
        cur.next = ListNode(total % 10)
        next = total // 10
        l1 = l1.next
        cur = cur.next
    while l2:
        total = l2.val + next
        cur.next = ListNode(total % 10)
        next = total // 10
        l2 = l2.next
        cur = cur.next
    if next:
        cur.next = ListNode(next)
    return res.next


l1 = listToNode([2, 4, 3])
l2 = listToNode([5, 6, 4])
print(nodeToList(addTwoNumbers(l1, l2)))
